<template>
    <div>
        <el-button type="primary" size="default" @click="showAddSpuForm" icon="ele-Plus" :disabled="!category3Id">添加SPU</el-button>
        <el-table :data="spuList" border stripe style="margin: 20px 0;">
            <el-table-column label="序号" type="index" width="80" align="center"> </el-table-column>
            <el-table-column label="SPU名称" prop="spuName"></el-table-column>
            <el-table-column label="SPU描述" prop="description"></el-table-column>
            <el-table-column label="操作">
                <template v-slot="{row,$index}">
                    <el-button type="success" size="small" icon="ele-Plus" title="添加sku" @click="showAddSkuForm(row)"></el-button>
                    <el-button type="warning" size="small" icon="ele-Edit"  title="修改spu" @click="showUpdateSpuForm(row)"></el-button>
                    <el-button type="info" size="small" icon="ele-InfoFilled" title="查看sku列表" @click="showSkuList(row)" ></el-button>
                    <el-button type="danger" size="small" icon="ele-Delete"  title="删除" @click="deleteSpu(row)"></el-button>      
                </template>
            </el-table-column>  

        </el-table> 
        
        <el-pagination
        @current-change="getSpuList"
        @size-change="handleSizeChange"
        style="margin-top:20px"
        v-model:current-page="page"
        v-model:page-size="limit"
        :total="total"
        :pager-count="9"
        :page-sizes="[3,5,7,10]"
        layout=" prev, pager, next, jumper,->,sizes,total"
    />

    <el-dialog :before-close="beforClose" v-model="dialogTableVisible" :title="`${spu.spuName}的sku列表`">
      <el-table :data="skuList" v-loading="loading">
        <el-table-column  label="名称" prop="skuName"/>
        <el-table-column  label="价格" prop="price"/>
        <el-table-column  label="重量" prop="weight"/>
        <el-table-column  label="默认图片">
          <template v-slot="{row,$index}">
            <img :src="row.skuDefaultImg" alt="" style="width:100px;height:80px">
          </template>
        </el-table-column> 
      </el-table>
    </el-dialog>
        
    </div>
</template>

<script lang="ts">
    import { defineComponent } from 'vue';

    export default defineComponent({
        name:'SpuList'
    })
</script>

<script setup lang="ts">
import {ref,watch} from 'vue'
import {useCategoryStore} from '@/stores/category'
import { storeToRefs } from 'pinia';
import spuApi, { type SpuData } from '@/api/spu'
import { ElMessage } from 'element-plus';
import type { SkuData } from '@/api/sku';
import skuApi from '@/api/sku'

const page = ref(1)
const limit = ref(6)
const total = ref(0)
const categoryStore = useCategoryStore()
const {category3Id} = storeToRefs(categoryStore)
const spuList = ref<SpuData[]>([]) 

// 专门用来发请求获取spu列表的
const getSpuList = async (num:number = 1) => {
  page.value = num
  try {
    const result = await spuApi.getPageList(page.value,limit.value,category3Id.value as number)
    spuList.value = result.records
    total.value = result.total
  } catch (error) {
    ElMessage.error('获取spu列表失败')
  }
}
// 修改每页数量的回调
const handleSizeChange = (size:number)=>{
    limit.value = size
    getSpuList()
}

// 对其进行监听 
watch(category3Id,(newVal,oldVal) => {
    if(newVal !== ''){  // 当新的值不为空也就是正常数据时 获取当前的数据
        // 发请求
        getSpuList()
    }else{
        // 当没有点到为空的时候时候的话清空之前获取的数据
        spuList.value = []
    }
},{
    immediate:true //同时进行深度监听 
})



const emits = defineEmits(['changeState'])

// 点击添加spu按钮
// const showAddSpuForm = () => {
//   emits('changeState',2)
// }

const showAddSpuForm = () => {
    emits('changeState',2) //发送事件传值
}
// 点击修改spu按钮
const showUpdateSpuForm = (row:SpuData) => {
    emits('changeState',2,row) // 发送事件传值
}
// const showUpdateSpuForm = (row:SpuData) => {
//   emits('changeState',2,row)
// }

// 删除
const deleteSpu = async(row:SpuData)=>{
    try {
        await spuApi.deleteSpu(row.id as number)
        ElMessage.success('删除成功')
        getSpuList()
    } catch (error) {
        ElMessage.error('删除失败')
    }
}
// 添加
const showAddSkuForm = (row:SpuData) => {
  emits('changeState',3,row)
}


// 点击查看sku列表的按钮逻辑
const dialogTableVisible = ref(false)
// 动态展示的数据
const spu = ref<SpuData>({
  spuName: '',
  description: '',
  category3Id: '',
  tmId: '',
  spuSaleAttrList: [],
  spuImageList: []
})
const skuList = ref<SkuData[]>([])
const loading = ref(false)

const showSkuList = async (row:SpuData) => {
  dialogTableVisible.value = true
  spu.value = row
  loading.value = true
  try {
    const result = await skuApi.findBySpuId(row.id as number)
    skuList.value = result
  } catch (error) {
    ElMessage.error('获取sku的列表失败')
  } finally {
    loading.value = false
  }
}

// 解决loading的时候上一次数据还显示的问题
const beforClose = () => {
  skuList.value = []
  loading.value = false
  dialogTableVisible.value = false
}
</script>

<style lang="less" scoped>

</style>